package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.xingkeduo.pojo.table.CraftsmanLeavingTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.LeavingTypeEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 一线员工离职信息封装类
 *
 * @author 高杰
 * @time 2019年1月8日 下午5:27:05
 * @description
 */
@Component
public class CraftsmanLeavingJTO extends BaseJson {
    public static CraftsmanLeavingTO createTableForNew(String json) throws Throwable {
        CraftsmanLeavingTO table = new CraftsmanLeavingTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setCraftsmanId(validPrimary(root, "craftsmanid", YesNoEnum.YES))
                .setReason(validReason(root, YesNoEnum.NO)).setRemark(validRemark(root, YesNoEnum.NO))
                .setType(validType(root, YesNoEnum.YES)).setSalaryDate(validSalaryDate(root,YesNoEnum.YES));

        return table;
    }

    public static LeavingTypeEnum validType(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "type";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!LeavingTypeEnum.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return LeavingTypeEnum.getLeaveType(value);
        }
        return null;
    }

    public static String validReason(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "reason";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 100)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validRemark(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "remark";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 100)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static Date validSalaryDate(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "salaryDate";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);
        if (node.has(key)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String value = node.path(key).asText().trim();
            Date date = sdf.parse(value);
            if (date == null) {
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            }
            return date;
        }
        return null;
    }
}
